// this script code for Tab Changer;
// date - 2012.01.05
// 1. object create
// 2. init Tab
// 3. parse handler(use addEvent module)
// 4. state - click : all hide and show selected tab
// 5. state - over : check selected tab, changing class name with " _ov_ "
// 6. state - out : chcek this tab has " _ov_ " and remove " _ov_ " and class name

Fsz.Component.add("TabChange", Fsz.Class({
 // Constructor
	initialize : function (tabInfo)
	{
		this.tabInit  = {
			type	: "click",
			id      : "tabArea", 
			initNum : 0, 
			tag     : "span", 
			cls     : "onThis"	
		}
		typeof tabInfo == "undefined" ? this.tabInit : this.tabInit = tabInfo;
		this._etype   = this.tabInit.type;
		this._obj     = $(this.tabInit.id);
		this._class   = this.tabInit.tabClass;
		this._tabs    = this.getTabs();
		this._tabSize = this._tabs.length;
		this._num     = this.tabInit.initNum;
		this._cls     = this.tabInit.cls;
		this._over 	  = false;
		this.callbackArr = [];
		
		this.initab();
	},
	getTabs : function(){
		var len = this._obj.$$("a").length, i, reArr = [];
		this.abArr = []; // shadow tabs
		
		for(i = 0; i < len; i++){
			if(this._obj.$$("a")[i].className.indexOf(this._class) > -1){
				reArr.push(this._obj.$$("a")[i].parent());
			}else if(this._obj.$$("a")[i].parent().className.indexOf(this._class) > -1){
				reArr.push(this._obj.$$("a")[i].parent());
				this.abArr.push(this._obj.$$("a")[i]);
			}
		}
		return reArr;
	},
	initab : function()
	{
		for(var j = 0;j<this._tabSize;j++)
		{
			$(this._tabs[j]).removeClassName(this._cls);
			this._tabs[j].nextElement().hide();
		}
		
		var n = this.scanClass(this._tabs[this._num],this._cls);
		
		if (!n)
		{
			this._tabs[this._num].addClassName(this._cls);
			this._tabs[this._num].nextElement().show();
		}
		
		this.sethandle();
	},
	
	sethandle : function()
	{
		var i, obj;
		
		for (i = 0; i < this._tabSize; i++)
		{
			obj = this._tabs[i];
			Event.observe(this._tabs[i], this._etype, this.parseEvt.bind(this, obj));
			Event.observe(this._tabs[i], "mouseout", this.outEvt.bind(this, obj));
			if(this._etype == "mouseover"){
				Event.observe(this.abArr[i], "focus", this.parseEvt.bind(this, obj));
				//Event.observe(this._tabs[i], "blur", this.outEvt.bind(this, obj));
			}
		}	
	},
	
	parseEvt : function(o)
	{
		if(event.type.indexOf("over") != -1){
			var self = this;
			o._over =true;
			setTimeout(function(){
				if(o._over){ self.runnable(self,o); }
				else {
				
				}
			},300);
		}
		else{
			this.runnable(this,o);		
		}
	},
	
	runnable : function(self,o){
		
		var j,
			tab = self._tabs;
			
		for (j = 0; j < self._tabSize; j++)
		{
			tab[j].removeClassName(self._cls).removeClassName("_ov");
			tab[j].nextElement().hide();
		}
		o.addClassName(self._cls);
		o.nextElement().show();
		
		if(this.tabInit.callback != undefined){
			self.callbackArr[this.tabInit.callback](o);
		}
	},
	
	outEvt : function(o)
	{
		o._over = false;
	},
	
	scanClass : function(o,cls){
		
		
		var ca  = o.getClassName(),
			len = ca.length,
			n   = false,
			i;
		
		for (i = 0; i < len; i++)
		{
			if (ca[i] === cls) n = true;
		}
		
		return n;
		
	},
	
	addCallBack : function(name,fc){
		this.callbackArr[name] = fc;
	}/*,
	
	setData : function(res,o){
	
		var i, 
			temp;
		for (i = 0; i < this._tabSize; i++)
		{
			if (this._tabs[i] === o) temp = i;
		}
		
		this._tabs[temp].nextElement().update(res[temp].contents);
	}*/
}));


